package post

import (
	"encoding/json"
	"io/ioutil"
	"jkrb_v2.0/module"
	"log"
	"net/http"
	"net/url"
	"strings"
)

func Post_Message(desc module.UserDesc) (bool, string) {
	form := url.Values{}
	/*
		_ = writer.WriteField("pcc", pcc)
			_ = writer.WriteField("gps", gps)
			_ = writer.WriteField("location", location)
			_ = writer.WriteField("status", "0")
			_ = writer.WriteField("tmp", "0")
			_ = writer.WriteField("contact", "0")
	*/
	form.Add("pcc", desc.Pcc)
	form.Add("gps", desc.Gps)
	form.Add("location", desc.Location)
	form.Add("status", "0")
	form.Add("tmp", "0")
	form.Add("contact", "0")
	req, err := http.NewRequest(http.MethodPost, "https://xg.nyist.vip/v1/trace/Student/dailyreportadd", strings.NewReader(form.Encode()))
	if err != nil {
		log.Println(err)
		return false, err.Error()
	}
	req.Header.Add("token", desc.Token)
	req.Header.Add("appid", "1")

	req.Form = form
	cli := http.Client{}
	resp, err := cli.Do(req)
	if err != nil {
		log.Println(err)
		return false, err.Error()
	}
	body, err := ioutil.ReadAll(resp.Body)
	var re module.Res
	err = json.Unmarshal(body, &re)
	if err != nil {
		log.Println(err)
		return false, err.Error()
	}
	if re.Code == 0 {
		return true, re.Msg
	} else {
		return false, re.Msg
	}
}
